#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>

#include <stdio.h>
#include <libpq-fe.h>

int main() {
    // 定义连接参数的键和值
    const char *keywords[] = {"host", "port", "user", "password", "dbname", NULL};
    const char *values[] = {"localhost", "db_port", "db_user", "user_passwd", "db_name", NULL};

    // 使用 PQconnectdbParams 建立连接
    PGconn *conn = PQconnectdbParams(keywords, values, 1);
    if (PQstatus(conn) != CONNECTION_OK) {
        fprintf(stderr, "Connection failed: %s\n", PQerrorMessage(conn));
        PQfinish(conn); // 释放连接资源
        return 1;
    }

    printf("Connection successful! \n");

    // 进行其他数据库操作...

    // 关闭数据库连接
    PQfinish(conn);
    return 0;
}